package com.dd.cloud.user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.cloud.user.entity.goods.ConversionGood;
import com.dd.cloud.user.entity.goods.GoodsExchange;
import com.dd.cloud.user.req.goods.ConversionGoodReq;
import com.dd.cloud.user.req.goods.GoodsExchangeAddFrom;
import com.dd.cloud.user.req.goods.GoodsExchangeReq;
import com.dd.cloud.user.req.goods.GoodsExchangeUpdateFrom;
import com.dd.cloud.user.service.goods.IConversionGoodService;
import com.dd.cloud.user.service.goods.IGoodsExchangeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * <p>
 * 商品积分兑换表 前端控制器
 * </p>
 *
 * @author czg
 * @since 2024-01-17
 */
@RestController
@RequestMapping("/exchange")
public class GoodsExchangeController {

    @Autowired
    private IGoodsExchangeService goodsExchangeService;

    @Autowired
    private IConversionGoodService conversionGoodRecordService;


    /**
     * 查询商品列表
     *
     * @param req
     */
    @GetMapping("/goods/page")
    public ResponseEntity<Page<GoodsExchange>> queryGoodsExchangePage(@Valid GoodsExchangeReq req) {
        Page<GoodsExchange> page = goodsExchangeService.queryGoodsExchangePage(req);
        return ResponseEntity.ok(page);
    }

    /**
     * 添加商品
     * @param from
     */
    @PostMapping("/goods/add")
    public ResponseEntity<Void> addGoodsExchange(@Valid @RequestBody GoodsExchangeAddFrom from) {
        goodsExchangeService.addGoodsExchange(from);
        return ResponseEntity.ok().build();
    }

    /**
     * 编辑商品
     * @param from
     */
    @PostMapping("/goods/update")
    public ResponseEntity<Void> updateGoodsExchange(@Valid @RequestBody GoodsExchangeUpdateFrom from) {
        goodsExchangeService.updateGoodsExchange(from);
        return ResponseEntity.ok().build();
    }


    /**
     * 修改状态
     *
     * @param goodId 商品id
     * @param status 状态 1.正常 -1.删除
     */
    @PostMapping("/goods/status")
    public ResponseEntity<Void> updateGoodsExchangeStatus(@NotNull(message = "商品id不能为空") Integer goodId,
                                                          @NotNull(message = "状态不能为空") Integer status) {
        goodsExchangeService.updateGoodsExchangeStatus(goodId, status);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询详情
     *
     * @param goodId 商品id
     */
    @GetMapping("/goods/detail")
    public ResponseEntity<GoodsExchange> queryGoodsExchangeDetail(@RequestParam("goodId") Integer goodId) {
        GoodsExchange goodsExchange = goodsExchangeService.queryGoodsExchangeDetail(goodId);
        return ResponseEntity.ok(goodsExchange);
    }

    /**
     * 上下移动
     *
     * @param flag   1.上移 2.下移
     * @param goodId 商品id
     */
    @PostMapping("/goods/move")
    public ResponseEntity<Void> moveGoodsExchange(@NotNull(message = "商品Id不能为空") Integer goodId,
                                                  @NotNull(message = "上下移动状态不能为空") Integer flag) {
        goodsExchangeService.moveGoodsExchange(goodId, flag);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改上下架
     *
     * @param goodId 商品id
     * @param isShow 状态 0.下架 1.上架
     */
    @PostMapping("/goods/isShow")
    public ResponseEntity<Void> updateGoodsExchangeIsShow(@NotNull(message = "商品id不能为空") Integer goodId,
                                                          @NotNull(message = "状态不能为空") Integer isShow) {
        goodsExchangeService.updateGoodsExchangeIsShow(goodId, isShow);
        return ResponseEntity.ok().build();
    }


    /**
     * 查询积分兑换(分页)
     * @param req
     */
    @GetMapping("/conversion/page")
    public ResponseEntity<Page<ConversionGood>> queryConversionGoodRecordPage(@Valid ConversionGoodReq req) {
        Page<ConversionGood> page = conversionGoodRecordService.queryConversionGoodRecordPage(req);
        return ResponseEntity.ok(page);
    }

    /**
     * 查询兑换详情
     * @param id
     */
    @GetMapping("/conversion/detail")
    public ResponseEntity<ConversionGood> queryConversionGoodRecordDetail(@Param("id") Integer id) {
        return ResponseEntity.ok(conversionGoodRecordService.queryConversionGoodRecordDetail(id));
    }


    /**
     * 修改状态
     * @param id
     * @param status
     */
    @PostMapping("/conversion/send")
    public ResponseEntity<Void> sendConversionGoods(@NotNull(message = "兑换id不能为空") Integer id,
                                                    @NotNull(message = "状态不能为空") Integer status) {
        conversionGoodRecordService.sendConversionGoods(id,status);
        return ResponseEntity.ok().build();
    }


    /**
     * 导出兑换列表excel
     * @param req
     */
    @GetMapping("/conversion/excel")
    public ResponseEntity<Void> exportConversionGoods(ConversionGoodReq req, HttpServletResponse response) {
        conversionGoodRecordService.exportConversionGoods(req, response);
        return ResponseEntity.ok().build();
    }


    /**
     * 商品兑换
     * @param goodId
     * @param num
     * @param phone
     */
    @PostMapping("/doctor/goods")
    public ResponseEntity<Map<Integer,String>> exchangeDoctorGoods(@NotNull(message = "商品id不能为空") Integer goodId,
                                                                   @NotNull(message = "商品消耗积分不能为空") Integer num,
                                                                   @NotNull(message = "兑换手机号不能为空") String phone) {
        Map<Integer,String> map = goodsExchangeService.exchangeDoctorGoods(goodId, num, phone);
        return ResponseEntity.ok(map);
    }

    /**
     * 查询商品列表
     *
     * @param req
     */
    @PostMapping("/doctor/goods/page")
    public ResponseEntity<Page<GoodsExchange>> queryDoctorGoodsPage(@Valid GoodsExchangeReq req) {
        Page<GoodsExchange> page = goodsExchangeService.queryDoctorGoodsPage(req);
        return ResponseEntity.ok(page);
    }

    /**
     * 查询我的兑换订单
     * @param req
     */
    @GetMapping("/doctor/exchange/order/page")
    public ResponseEntity<Page<ConversionGood>> queryConversionGoodPage(@Valid ConversionGoodReq req) {
        Page<ConversionGood> page = conversionGoodRecordService.queryConversionGoodPage(req);
        return ResponseEntity.ok(page);
    }


}

